<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Quanlydanhmuc extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 Thu
	 */
	function __construct()
    {
        parent::__construct();	
		$this->data['tieude'] = "Quản lý danh mục";
		$this->loadadmin();
	} 
	
	public function index()
	{	
		$this->load->model('Danhmuc_model');
		$this->data['danhmucsdrop'] = $this->Danhmuc_model->getresult();
		$this->data['danhmucs'] = $this->Danhmuc_model->getresult_index();
		//print_r($this->data);
		$this->data['danhmuc']= $this->input->get('dm');
		$this->load->view('danhmuc/danhmuc', $this->data);
		$this->load->view('layout/footer');		
	}
	
	public function them()
	{
		$this->load->model('Danhmuc_model');
		$this->data['danhmucs'] = $this->Danhmuc_model->getresult_index();

		$this->load->view('danhmuc/themdanhmuc', $this->data);
		$this->load->view('layout/footer');
	}
	
	public function submit_them()
	{
		$this->load->library('form_validation');
		$this->load->model('Danhmuc_model');
		$this->data['danhmucs'] = $this->Danhmuc_model->getresult_index();
		$fileHinhDM = "fileHinhDM";
		
		if($this->form_validation->run('themdanhmuc') == false){
			$this->load->view('danhmuc/themdanhmuc', $this->data);
		}
		elseif(!isset($_FILES[$fileHinhDM])||($_FILES[$fileHinhDM]['error'] == 4))
			{	
				$this->data['thongtindanhmuc']=	$this->input->post();	
				$this->data['danhmuc']=$this->data['thongtindanhmuc']['madm'];
				$this->data['img']['error_upload']='Chưa chọn hình!';
				$this->load->view('danhmuc/themdanhmuc', $this->data);
			}
			else 
			{	
				$this->data['img']=	$this->upload_process($fileHinhDM);
				$this->data['thongtindanhmuc']=	$this->input->post();
				//print_r($this->data['thongtindanhmuc']);
				
				if(!empty($this->data['img']['image_full_url']))
				{
					$danhmuc = array(
						'tendanhmuc'=>$this->data['thongtindanhmuc']['tendanhmuc'],
						//'hinh'=>"abc.jpg",
						'hinh'=>$this->data['img']['image_full_url'],
						'madm_cha'=>$this->data['thongtindanhmuc']['madm_cha'],
						'tendanhmuc_en'=>$this->data['thongtindanhmuc']['tendanhmuc_en'],
						);
						
					$this->data['danhmuc'] = $danhmuc;		
					$this->load->model('Danhmuc_model');
					$id = $this->Danhmuc_model->insert($this->data['danhmuc']);
						
					$this->data['thongbao'] = "Thêm thành công!".$this->data['thongtindanhmuc']['tendanhmuc']."!";	
				}
				$this->load->view('danhmuc/themdanhmuc',$this->data);
			}	
		$this->load->view('layout/footer');
	}
	
	public function upload_process($upload_field) 
	{
		$result = array();
		
		// Upload file
		$this->load->config('upload.php');
		$upload_config = $this->config->item('upload');
		
		$this->load->library('upload', $upload_config);
		$this->upload->initialize($upload_config);
		
		if (!$this->upload->do_upload($upload_field))
		{
			// Upload bị lỗi
			$result['error_upload'] = $this->upload->display_errors();
			$result['image_full_url'] = '';
		}
		else
		{
			// Upload file successfull
			$result['upload_data'] = $this->upload->data();
			
			if (isset($result['upload_data']['is_image']) && $result['upload_data']['is_image'] == true) {
				// File vừa tải lên là file ảnh hợp lệ
				$result['image_full_url'] =  $upload_config['upload_path'] . $result['upload_data']['file_name'];
			}
			else {
				// File vừa tải lên không phải là file ảnh hợp lệ
				$result['image_full_url'] = '';
			}
		}
		
		return $result;
	}
	
	public function sua($madm)
	{		
		$this->load->library('form_validation');
		$this->load->model('Danhmuc_model');
		
		$this->data['danhmucs'] = $this->Danhmuc_model->getresult_index();
				
		$this->data['thongtindanhmuc'] = $this->input->post();
		
		if(!empty($this->data['thongtindanhmuc']))
		{
			if($this->form_validation->run('themdanhmuc') )
			{
				$fileHinhDM = "fileHinhDM";
				
				if(!($_FILES[$fileHinhDM]['error'] == 4))
				{	
					$this->data['img'] = $this->upload_process($fileHinhDM);	
					
					if(!empty($this->data['img']['image_full_url']))
					{
						$this->data['thongtindanhmuc'] = $this->input->post();	
						$this->data['thongtindanhmuc']['hinh'] = $this->data['img']['image_full_url'];
						$this->Danhmuc_model->xoahinhdanhmuc($madm);		
						$this->Danhmuc_model->update($this->data['thongtindanhmuc'], $madm);
					}
				}
				else
				{
					$this->data['thongtindanhmuc'] = $this->input->post();
					$this->Danhmuc_model->update($this->data['thongtindanhmuc'], $madm);
				}
				
				$this->data['thongbao'] = "Sửa thành công!".$this->data['thongtindanhmuc']['tendanhmuc']."!";		
			}	
		}
		
		$this->data['danhmuc'] = $this->Danhmuc_model->getdanhmuc($madm);// 1 array
		
		$this->load->view('danhmuc/suadanhmuc', $this->data);
		$this->load->view('layout/footer');
	}
	
	public function xoa($madm)
	{
		$this->load->model('Danhmuc_model');
		$this->Danhmuc_model->delete($madm);
		$dt= $this->input->get();
		
		if(!empty($dt['key']) && !empty($dt['madm_cha']))	
			redirect("/Quanlydanhmuc/index/?key={$dt['key']}&madm_cha={$dt['madm_cha']}", 'refresh');
		else if(!empty($dt['key']))
				redirect("/Quanlydanhmuc/index/?key={$dt['key']}", 'refresh');
			else if(!empty($dt['madm_cha']))
					redirect("/Quanlydanhmuc/index/?madm_cha={$dt['madm_cha']}", 'refresh');
				else 
    				redirect("/Quanlydanhmuc/index/", 'refresh');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */